package com.zt.bigdata.practise.time;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 *
 */
public class DateTimeFormat {

    private static final ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");

    private static LocalDateTime LOCAL_DATETIME = LocalDateTime.of(2019, 1, 24, 19, 14, 29,980);
    private static Long TIMESTAMP = 1548328469980L;

    private static String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    private static String yyyyMMddhhmmssa = "yyyy-MM-dd hh:mm:ss a";
    private static String yyyyMMddhhmmss =  "yyyy-MM-dd hh:mm:ss";
    private static String yyyyMMddTHHmmssSSSZ =  "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    private static String yyyyMMddTHHmmssSSSXXX =  "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    /**
     * 使用 SimpleDateFormat 时间戳 转 字符串
     *
     * @param timestamp
     * @param format
     * @return
     */
    public static String long2String(long timestamp, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date(timestamp));
    }

    /**
     * 使用 SimpleDateFormat 字符串 转 时间戳
     *
     * @param timeString
     * @param format
     * @return
     */
    public static Long string2Long(String timeString, String format) {
        Long timestamp = null;
        DateFormat dateFormat = new SimpleDateFormat(format);
        try {
            timestamp = dateFormat.parse(timeString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }

    public static Date string2Date(String timeString, String format) {
        return new Date(string2Long(timeString, format));
    }

    public static String date2String(Date date, String format) {
        return long2String(date.getTime(), format);
    }

    /**
     * LocalDateTime 转 TIMESTAMP
     *
     * @param localDateTime
     * @return
     */
    public static Long localDateTime2Long1(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * LocalDateTime 转 TIMESTAMP
     *
     * @param localDateTime
     * @return
     */
    public static Long localDateTime2Long2(LocalDateTime localDateTime) {
        ZonedDateTime zdt = localDateTime2Zdt(localDateTime, ZONE_ID);
        return zdt.toInstant().toEpochMilli();
    }

    /**
     * LocalDateTime 转 string
     *
     * @param localDateTime
     * @param format
     * @return
     */
    public static String localDateTime2String(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        ZonedDateTime zdt = localDateTime2Zdt(localDateTime, ZONE_ID);
        return zdt.format(formatter);
    }

    /**
     * LocalDateTime 转 ZonedDateTime
     *
     * @param localDateTime
     * @param zoneId
     * @return
     */
    public static ZonedDateTime localDateTime2Zdt(LocalDateTime localDateTime, ZoneId zoneId) {
        return localDateTime.atZone(zoneId);
    }

    /**
     * string 转 LocalDateTime
     *
     * @param timeString
     * @param format
     * @return
     */
    public static LocalDateTime string2LocalDateTime(String timeString, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(timeString, formatter);
    }


    /**
     * string 转 Long
     *
     * @param timeString
     * @param format
     * @return
     */
    public static Long string2LongByJdk8(String timeString, String format) {
        LocalDateTime localDateTime = string2LocalDateTime(timeString, format);
        return localDateTime2Long1(localDateTime);
    }


    /**
     * long 转 string jdk 1.8
     *
     * @param timestamp
     * @param format
     * @return
     */
    public static String long2StringByJdk8(long timestamp, String format) {
        return localDateTime2String(long2LocalDateTime(timestamp), format);
    }

    /**
     * long 转 LocalDateTime
     *
     * @param timestamp
     * @return
     */
    public static LocalDateTime long2LocalDateTime(long timestamp) {
        return Instant.ofEpochMilli(timestamp).atZone(ZONE_ID).toLocalDateTime();
    }

    public static void main(String[] args) {



        System.out.println("\n字符串 转 LocalDateTime");
        System.out.println(string2LocalDateTime("2019-01-24 19:14:29", yyyyMMddHHmmss));
        System.out.println(string2LocalDateTime("2019-01-24 07:14:29 上午", yyyyMMddhhmmssa));
        System.out.println(string2LocalDateTime("2019-01-24T19:14:29.000+0800", yyyyMMddTHHmmssSSSZ));
        System.out.println(string2LocalDateTime("2019-01-24T19:14:29.000+08:00", yyyyMMddTHHmmssSSSXXX));


        System.out.println("\n使用 LocalDateTime  字符串 转 long");
        System.out.println(string2LongByJdk8("2019-01-24 19:14:29", yyyyMMddHHmmss));
        System.out.println(string2LongByJdk8("2019-01-24 07:14:29 上午", yyyyMMddhhmmssa));
        System.out.println(string2LongByJdk8("2019-01-24T19:14:29.000+0800", yyyyMMddTHHmmssSSSZ));
        System.out.println(string2LongByJdk8("2019-01-24T19:14:29.000+08:00", yyyyMMddTHHmmssSSSXXX));

        System.out.println("\n使用 LocalDateTime  long 转 字符串");
        System.out.println(long2StringByJdk8(TIMESTAMP, yyyyMMddHHmmss));
        System.out.println(long2StringByJdk8(TIMESTAMP, yyyyMMddhhmmssa));
        System.out.println(long2StringByJdk8(TIMESTAMP, yyyyMMddTHHmmssSSSZ));
        System.out.println(long2StringByJdk8(TIMESTAMP, yyyyMMddTHHmmssSSSXXX));




    }
}
